package com.test.mylotterysystem.comment.Pojo;

import com.test.mylotterysystem.comment.errorCode.ErrorCode;
import com.test.mylotterysystem.comment.errorCode.GlobalErrorCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommentResult<T> {
    private Integer code;
    private String errMsg;
    private T data;

    /**
     * 成功时返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> CommentResult<T> success(T data) {
        CommentResult<T> ret = new CommentResult<>();
        ret.setCode(GlobalErrorCode.SUCCESS.getCode());
        ret.setErrMsg("");
        ret.setData(data);
        return ret;
    }

    /**
     * 错误时返回
     */
    public static <T> CommentResult<T> fail(int code,String errorMsg) {
        CommentResult<T> ret = new CommentResult<>();
        ret.setCode(code);
        ret.setErrMsg(errorMsg);
        ret.setData(null);
        return ret;
    }

    public static <T> CommentResult<T> fail(ErrorCode errorCode) {
        CommentResult<T> ret = new CommentResult<>();
        ret.setCode(errorCode.getCode());
        ret.setErrMsg(errorCode.getErrorMsg());
        ret.setData(null);
        return ret;
    }

}
